package com.ctshk.rpc.booking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.booking.dto.outside.ship.query.SupplierOutsideOrderShipDTO;
import com.ctshk.rpc.booking.dto.outside.ship.query.SupplierOutsideOrderShipTouristDTO;
import com.ctshk.rpc.booking.dto.outside.ship.query.SupplierOutsideOrderShipTouristTypeDTO;
import com.ctshk.rpc.booking.entity.SupplierOutsideOrderShip;
import com.ctshk.rpc.booking.entity.SupplierOutsideOrderShipTourist;
import com.ctshk.rpc.booking.entity.SupplierOutsideOrderShipTouristType;
import com.ctshk.rpc.booking.mapper.SupplierOutsideOrderShipMapper;
import com.ctshk.rpc.booking.mapper.SupplierOutsideOrderShipTouristMapper;
import com.ctshk.rpc.booking.mapper.SupplierOutsideOrderShipTouristTypeMapper;
import com.ctshk.rpc.booking.req.outside.ship.save.SupplierOutsideOrderShipReq;
import com.ctshk.rpc.booking.service.ISupplierOutsideOrderShipService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 站外船票出行信息 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-09
 */
@DubboService
public class SupplierOutsideOrderShipServiceImpl implements ISupplierOutsideOrderShipService {
    @Autowired
    SupplierOutsideOrderShipMapper supplierOutsideOrderShipMapper;
    @Autowired
    SupplierOutsideOrderShipTouristMapper supplierOutsideOrderShipTouristMapper;
    @Autowired
    SupplierOutsideOrderShipTouristTypeMapper supplierOutsideOrderShipTouristTypeMapper;

    @Override
    public void save(Long supplierOrderId, Long userId, LocalDateTime nowDateTime, List<SupplierOutsideOrderShipReq> supplierOutsideOrderShipReqList) {
        if (supplierOrderId != null) {
            //删除
            QueryWrapper<SupplierOutsideOrderShip> outsideOrderShipWrapper = new QueryWrapper<>();
            outsideOrderShipWrapper.lambda().eq(SupplierOutsideOrderShip::getOrderId, supplierOrderId);
            supplierOutsideOrderShipMapper.delete(outsideOrderShipWrapper);
            QueryWrapper<SupplierOutsideOrderShipTouristType> outsideOrderShipTouristTypeWrapper = new QueryWrapper<>();
            outsideOrderShipTouristTypeWrapper.lambda().eq(SupplierOutsideOrderShipTouristType::getOrderId, supplierOrderId);
            supplierOutsideOrderShipTouristTypeMapper.delete(outsideOrderShipTouristTypeWrapper);
            QueryWrapper<SupplierOutsideOrderShipTourist> outsideOrderShipTouristWrapper = new QueryWrapper<>();
            outsideOrderShipTouristWrapper.lambda().eq(SupplierOutsideOrderShipTourist::getOrderId, supplierOrderId);
            supplierOutsideOrderShipTouristMapper.delete(outsideOrderShipTouristWrapper);
        }
        //新增飞机信息
        supplierOutsideOrderShipReqList.forEach(outsideOrderShipReq -> {
            SupplierOutsideOrderShip supplierOutsideOrderShip = EntityUtil.copy(outsideOrderShipReq, SupplierOutsideOrderShip.class);
            supplierOutsideOrderShip.setId(SnowflakeIdWorker.nextId());
            supplierOutsideOrderShip.setOrderId(supplierOrderId);
            supplierOutsideOrderShip.setCreateId(userId);
            supplierOutsideOrderShip.setGmtCreate(nowDateTime);
            supplierOutsideOrderShip.setModifiedId(userId);
            supplierOutsideOrderShip.setGmtModified(nowDateTime);
            supplierOutsideOrderShip.setIsDeleted(IsDeletedCode.NO.getCode());
            supplierOutsideOrderShipMapper.insert(supplierOutsideOrderShip);
            if (CollectionUtils.isNotEmpty(outsideOrderShipReq.getSupplierOutsideOrderShipTouristTypeReqList())) {
                //新增旅客类型信息
                outsideOrderShipReq.getSupplierOutsideOrderShipTouristTypeReqList().forEach(outsideOrderShipTouristTypeReq -> {
                    SupplierOutsideOrderShipTouristType supplierOutsideOrderShipTouristType = EntityUtil.copy(outsideOrderShipTouristTypeReq, SupplierOutsideOrderShipTouristType.class);
                    supplierOutsideOrderShipTouristType.setId(SnowflakeIdWorker.nextId());
                    supplierOutsideOrderShipTouristType.setOrderId(supplierOrderId);
                    supplierOutsideOrderShipTouristType.setShipId(supplierOutsideOrderShip.getId());
                    supplierOutsideOrderShipTouristType.setCreateId(userId);
                    supplierOutsideOrderShipTouristType.setGmtCreate(nowDateTime);
                    supplierOutsideOrderShipTouristType.setModifiedId(userId);
                    supplierOutsideOrderShipTouristType.setGmtModified(nowDateTime);
                    supplierOutsideOrderShipTouristType.setIsDeleted(IsDeletedCode.NO.getCode());
                    supplierOutsideOrderShipTouristTypeMapper.insert(supplierOutsideOrderShipTouristType);
                    if (CollectionUtils.isNotEmpty(outsideOrderShipTouristTypeReq.getSupplierOutsideOrderShipTouristReqList())) {
                        outsideOrderShipTouristTypeReq.getSupplierOutsideOrderShipTouristReqList().forEach(outsideOrderShipTouristReq -> {
                            SupplierOutsideOrderShipTourist supplierOutsideOrderShipTourist = EntityUtil.copy(outsideOrderShipTouristReq, SupplierOutsideOrderShipTourist.class);
                            supplierOutsideOrderShipTourist.setId(SnowflakeIdWorker.nextId());
                            supplierOutsideOrderShipTourist.setOrderId(supplierOrderId);
                            supplierOutsideOrderShipTourist.setShipId(supplierOutsideOrderShip.getId());
                            supplierOutsideOrderShipTourist.setCreateId(userId);
                            supplierOutsideOrderShipTourist.setGmtCreate(nowDateTime);
                            supplierOutsideOrderShipTourist.setModifiedId(userId);
                            supplierOutsideOrderShipTourist.setGmtModified(nowDateTime);
                            supplierOutsideOrderShipTourist.setIsDeleted(IsDeletedCode.NO.getCode());
                            supplierOutsideOrderShipTouristMapper.insert(supplierOutsideOrderShipTourist);
                        });
                    }
                });
            }
        });
    }

    @Override
    public List<SupplierOutsideOrderShipDTO> selectByOrderId(Long id) {
        List<SupplierOutsideOrderShipDTO> supplierOutsideOrderShipDTOS = new ArrayList<>();
        //查询机票列表
        QueryWrapper<SupplierOutsideOrderShip> shipWrapper = new QueryWrapper();
        shipWrapper.lambda().eq(SupplierOutsideOrderShip::getOrderId, id);
        List<SupplierOutsideOrderShip> supplierOutsideOrderShips = supplierOutsideOrderShipMapper.selectList(shipWrapper);
        if (CollectionUtils.isNotEmpty(supplierOutsideOrderShips)) {
            supplierOutsideOrderShipDTOS = EntityUtil.copyList(supplierOutsideOrderShips, SupplierOutsideOrderShipDTO.class);
            supplierOutsideOrderShipDTOS.forEach(ship -> {
                //查询机票旅客类型列表
                QueryWrapper<SupplierOutsideOrderShipTouristType> shipTouristTypeWrapper = new QueryWrapper();
                shipTouristTypeWrapper.lambda().eq(SupplierOutsideOrderShipTouristType::getOrderId, id);
                shipTouristTypeWrapper.lambda().eq(SupplierOutsideOrderShipTouristType::getShipId, ship.getId());
                List<SupplierOutsideOrderShipTouristType> supplierOutsideOrderShipTouristTypes = supplierOutsideOrderShipTouristTypeMapper.selectList(shipTouristTypeWrapper);
                if (CollectionUtils.isNotEmpty(supplierOutsideOrderShipTouristTypes)) {
                    List<SupplierOutsideOrderShipTouristTypeDTO> supplierOutsideOrderShipTouristTypeDTOList = EntityUtil.copyList(supplierOutsideOrderShipTouristTypes, SupplierOutsideOrderShipTouristTypeDTO.class);
                    ship.setSupplierOutsideOrderShipTouristTypeDTOList(supplierOutsideOrderShipTouristTypeDTOList);
                    supplierOutsideOrderShipTouristTypeDTOList.forEach(shipTouristType -> {
                        //查询机票旅客列表
                        QueryWrapper<SupplierOutsideOrderShipTourist> shipTouristWrapper = new QueryWrapper();
                        shipTouristWrapper.lambda().eq(SupplierOutsideOrderShipTourist::getOrderId, id);
                        shipTouristWrapper.lambda().eq(SupplierOutsideOrderShipTourist::getTypeId, shipTouristType.getId());
                        List<SupplierOutsideOrderShipTourist> supplierOutsideOrderShipTourists = supplierOutsideOrderShipTouristMapper.selectList(shipTouristWrapper);
                        if (CollectionUtils.isNotEmpty(supplierOutsideOrderShipTourists)) {
                            List<SupplierOutsideOrderShipTouristDTO> supplierOutsideOrderShipTouristDTOList = EntityUtil.copyList(supplierOutsideOrderShipTourists, SupplierOutsideOrderShipTouristDTO.class);
                            shipTouristType.setSupplierOutsideOrderShipTouristDTOList(supplierOutsideOrderShipTouristDTOList);
                        }
                    });
                }
            });
        }
        return supplierOutsideOrderShipDTOS;
    }
}
